package com.mobile.check.activitys;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.BaseListActivity;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.ListItemLongClickListener;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;
import cn.droidlover.xdroidmvp.kit.NToast;
import com.mobile.check.beans.TaskInfo;
import com.mobile.check.app.R;
import com.mobile.check.present.TaskListPresent;
import com.mobile.check.dialogs.BottomItem;
import com.mobile.check.dialogs.BottomListDialog;
import com.mobile.check.dialogs.SubmitTipDialog;
import com.mobile.check.util.TaskTypeEnum;

/**
 * 类名: Task2ListActivity
 * 作者: 李学伟
 * 日期: 2020年10月20日 15:52
 * 描述:
 */
public class TaskListActivity extends BaseListActivity<TaskInfo, TaskListPresent> {

    boolean isEditModel;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.submitTv)
    Button submitTv;
    @BindView(R.id.uploadTv)
    Button uploadTv;
    boolean isLocalTask;
    @BindView(R.id.menuIv)
    ImageView menuIv;
    @BindView(R.id.importIv)
    ImageView importIv;
    private TaskTypeEnum targetTaskType;
    private List<TaskInfo> selectList = new ArrayList<>();



    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        titleTv.setText(targetTaskType.getName());
//        targetTaskType = TaskTypeEnum.CHECKTASK;
    }

    @Override
    public void initView() {
        super.initView();

        mBaseAdapter.setLongClickListener(new ListItemLongClickListener() {
            @Override
            public void itemLongClick(int position) {
                isEditModel = true;
                mBaseAdapter.notifyDataSetChanged();
            }
        });
        if (isLocalTask) {
            importIv.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public BaseRecyclerAdapter<TaskInfo> getAdapter() {
        return new BaseRecyclerAdapter<TaskInfo>(this) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_task;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                TaskInfo info = getItemByPosition(position);
                holder.setText(R.id.deviceNameTv, info.getTask_name());
                if (info.isLocalFile()) {
//                    holder.setVisibility(R.id.statusTv, View.VISIBLE);
                } else {
//                    holder.setText(R.id.statusTv, "已上传");
                    if (info.isHadUploaded()) {
                        holder.setVisibility(R.id.statusTv, View.VISIBLE);
                    } else {
                        holder.setVisibility(R.id.statusTv, View.INVISIBLE);
                    }
                }
                if (info.isErrorFile()) {
                    holder.setText(R.id.statusTv, "错误文件");
                }

                if (isEditModel) {
                    holder.setVisibility(R.id.selectIv, View.VISIBLE);
                    if (selectList.contains(info)) {
                        holder.setImageResDrawable(R.id.selectIv, R.drawable.select1);
                    } else {
                        holder.setImageResDrawable(R.id.selectIv, R.drawable.select0);
                    }
                }else{
                    holder.setVisibility(R.id.selectIv, View.VISIBLE);
                    holder.setImageResDrawable(R.id.selectIv,R.mipmap.rightarrow);
//                    holder.setVisibility(R.id.selectIv, View.INVISIBLE);
                }
            }
        };
    }

    @Override
    public void itemClick(int position) {
        TaskInfo info = mBaseAdapter.getItemByPosition(position);
        if (isEditModel) {
            if (selectList.contains(info)) {
                selectList.remove(info);
            } else {
                selectList.add(info);
            }
            checkSlelectCount();
        } else {
            if (!info.isHadImported()) {
                try {
                    getP().saveTaskInfo(info);
                } catch (Exception e) {
                    showToast("文件错误,请重新导入1！");
                    getP().saveCrash(e.getMessage());
                    e.printStackTrace();
                }
            } else {
                importSuccess(info);
            }
        }
    }


    @Override
    public int getContentViewId() {
        return R.layout.activity_tasklist;
    }


    @Override
    public void getDataFromServer() {
        getP().queryTaskInfo(isLocalTask, targetTaskType);
    }

    @Override
    public void getIntentData() {
        targetTaskType = (TaskTypeEnum)getIntent().getSerializableExtra("targetTaskType");
        isLocalTask = getIntent().getBooleanExtra("isLocalTask", false);
    }

    @Override
    public TaskListPresent newP() {
        return new TaskListPresent();
    }

    @OnClick({R.id.submitTv, R.id.menuIv, R.id.importIv, R.id.uploadTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.submitTv:
                if (selectList.size() == 0) {
                    NToast.shortToast(this, "请选择检测项");
                } else {
                    showDelDilaog();
                }
                break;
            case R.id.menuIv:
                showMenus();
                break;
            case R.id.importIv:
                TaskServerListActivity.lunch(this, targetTaskType.getType());
                break;
            case R.id.uploadTv:
                getP().startUpload(selectList);
                break;
        }
    }


    private void showDelDilaog() {
        SubmitTipDialog submitTipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
            @Override
            public void submitClick() {
                getP().del(selectList);
                mBaseAdapter.remove(selectList);
                selectList.clear();
                if (mBaseAdapter.getItemCount() == 0) {
                    isEditModel = false;
                    submitTv.setVisibility(View.GONE);
                    uploadTv.setVisibility(View.GONE);
                }
            }

            @Override
            public void cancelClick() {

            }
        });
        submitTipDialog.display("温馨提示", "您导入的相应文件及测试数据将被删除，是否继续？", "取消", "确定");
    }

    private void checkSlelectCount() {
        if (selectList.size() != 0) {
            submitTv.setVisibility(View.VISIBLE);
            if (!isLocalTask) {
                uploadTv.setVisibility(View.VISIBLE);
            }
        } else {
            submitTv.setVisibility(View.GONE);
            uploadTv.setVisibility(View.GONE);
        }
        mBaseAdapter.notifyDataSetChanged();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == 0x001) {
            getDataFromServer();
        }
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }

    public static void lunch(Context context, boolean isLocalTask,TaskTypeEnum targetTaskType) {
        Intent intent = new Intent(context, TaskListActivity.class);
        intent.putExtra("isLocalTask", isLocalTask);
        intent.putExtra("targetTaskType", targetTaskType);
        context.startActivity(intent);
    }

    @Override
    public int getTitleLayoutResId() {
        return 0;
    }

    @Override
    public void onRightClick(View view) {
        showMenus();
    }

    private void showMenus() {
        List<BottomItem> list = new ArrayList<>();
        if (!isEditModel) {
            list.add(new BottomItem("检测任务", TaskTypeEnum.CHECKTASK));
            list.add(new BottomItem("复检任务", TaskTypeEnum.REPEATTASK));
            list.add(new BottomItem("跟踪任务", TaskTypeEnum.TRACKTASK));
//            if (mBaseAdapter.getItemCount() != 0) {
//                list.add(new BottomItem("编辑任务", TaskTypeEnum.EDITKTASK));
//            }
        }
        BottomListDialog bottomListDialog = new BottomListDialog(this, new BottomListDialog.BottomListSelectListener<TaskTypeEnum>() {
            @Override
            public void itemClickCallback(TaskTypeEnum typeEnum) {
                switch (typeEnum) {
                    case CHECKTASK:
                    case REPEATTASK:
                    case TRACKTASK:
                        mBaseAdapter.removeAll();
                        targetTaskType = typeEnum;
                        titleTv.setText(typeEnum.getName());
                        getDataFromServer();
                        break;
                    case EDITKTASK:
                        isEditModel = true;
                        mBaseAdapter.notifyDataSetChanged();
                        break;
                    case CANCELTASK:
                        selectList.clear();
                        isEditModel = false;
                        submitTv.setVisibility(View.INVISIBLE);
                        mBaseAdapter.notifyDataSetChanged();
                        break;
                }
            }
        });
        bottomListDialog.disPlay(list);
    }

    public void importSuccess(TaskInfo taskInfo) {
        disLoadDialog();
        Intent intent = new Intent(this, TaskItemListActivity.class);
        intent.putExtra("taskInfo", taskInfo);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        if (isEditModel) {
            selectList.clear();
            isEditModel = false;
            submitTv.setVisibility(View.GONE);
            uploadTv.setVisibility(View.GONE);
            notifyDataSetChanged();
        } else {
            super.onBackPressed();
        }

    }
}

